package com.quanyan.place.service.impl;

import com.quanyan.common.utils.CommonUtils;
import com.quanyan.place.entity.TbBizPlace;
import com.quanyan.place.entity.TbBizPlaceMaxCardNo;
import com.quanyan.place.entity.TbBizPlaceMaxCardNoExample;
import com.quanyan.place.entity.TbCity;
import com.quanyan.place.mapper.TbBizPlaceMapper;
import com.quanyan.place.mapper.TbBizPlaceMaxCardNoMapper;
import com.quanyan.place.mapper.TbCityMapper;
import com.quanyan.place.service.CardNoService;
import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * Created by Kingson.chan on 2016/8/25 0025.
 */
@Service("cardNoService")
public class CardNoServiceImpl implements CardNoService {
    private static final Logger logger = LoggerFactory.getLogger(CardNoServiceImpl.class);

    @Autowired
    TbCityMapper tbCityMapper;

    @Autowired
    TbBizPlaceMaxCardNoMapper tbBizPlaceMaxCardNoMapper;

    @Autowired
    TbBizPlaceMapper tbBizPlaceMapper;

    @Override
    public String getCardNo(Map map) {
        try {
            Integer placeId = MapUtils.getInteger(map, "placeId");
            TbBizPlace tbBizPlace = tbBizPlaceMapper.selectByPrimaryKey(placeId);
            //获取场馆城市信息
            TbCity tbCity = tbCityMapper.selectByPrimaryKey(tbBizPlace.getCityId());
            String cityPY = tbCity.getPinyin();
            String placeString = CommonUtils.zeroFill(placeId % 1000000 + "", 6);
            int nextCard = getNextCardNo();
            if(nextCard > 0){
                String cardId = CommonUtils.zeroFill(nextCard + "", 10);
                return cityPY + placeString + cardId;
            }
            return null;
        }
        catch (Exception e){
            logger.warn(e.getMessage());
        }
        return null;
    }

    /**
     * 使用事物串行化，防止生成卡号冲突
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.SERIALIZABLE, rollbackFor = Exception.class)
    private  int getNextCardNo() {
        TbBizPlaceMaxCardNoExample tbBizPlaceMaxCardNoExample = new TbBizPlaceMaxCardNoExample();
        TbBizPlaceMaxCardNoExample.Criteria TbBizPlaceMaxCardNoCriteria = tbBizPlaceMaxCardNoExample.createCriteria();
        TbBizPlaceMaxCardNoCriteria.andMaxCardNoIsNotNull();
        List<TbBizPlaceMaxCardNo> rows = tbBizPlaceMaxCardNoMapper.selectByExample(tbBizPlaceMaxCardNoExample);
        int max_CardNo = rows.get(0).getMaxCardNo();

        TbBizPlaceMaxCardNoExample tbBizPlaceMaxCardNoExample1 = new TbBizPlaceMaxCardNoExample();
        TbBizPlaceMaxCardNoExample.Criteria TbBizPlaceMaxCardNoCriteria1 = tbBizPlaceMaxCardNoExample.createCriteria();
        TbBizPlaceMaxCardNoCriteria1.andMaxCardNoEqualTo(max_CardNo);
        TbBizPlaceMaxCardNo tbBizPlaceMaxCardNo = new  TbBizPlaceMaxCardNo();
        tbBizPlaceMaxCardNo.setMaxCardNo(max_CardNo + 1);
        int result = tbBizPlaceMaxCardNoMapper.updateByExampleSelective(tbBizPlaceMaxCardNo, tbBizPlaceMaxCardNoExample1);
        if (result == 1) {
            return max_CardNo + 1;
        }
        logger.warn("获取卡号失败");
        return 0;
    }
}
